import QuickTool from './QuickTool';
export default class ArrayTool {

    public static flatArray<T> (ar :Array<Array<T>>) : Array<T> {
        let re = new Array<T>()
        ar.forEach(element => {
          element.forEach(e => {
              re.push(e);
          });  
        });

        return re
    }

    public static copy<T> (ar: Array<T>): Array<T> {
        let re = new Array<T>()
        ar.forEach(element => {
            re.push(element)
        })
        return re
    }

    public static randomAndPop<T> (ar: Array<T>): T{
        let idx = QuickTool.randomNum(0, ar.length-1)
        let e = ar[idx]
        ar.splice(idx, 1)
        return e
    }

    public static randomOne<T> (ar: Array<T>): T{
        let idx = QuickTool.randomNum(0, ar.length-1)
        return ar[idx]
    }

    public static randomArray<T> (ar: Array<T>) : Array<T> {
        let copy = ar.concat([])

        // copy.sort((a: T, b: T)=>{
        //     return QuickTool.randomNum(-1,1)
        // })

        copy.sort(() => Math.random() - 0.5);


        return copy
    }

    public static indexOf<T>(ar: Array<T>, item:T, compareFn: (a: T, b: T) => boolean) : number {
        for (let i = 0; i < ar.length; i++) {
            const e = ar[i];
            if (compareFn(e, item)){
                return i;
            }
        }

        return -1
    }

    public static createLNArray (length: number): Array<number> {
        let re = new Array<number>()

        for (let i=0;i<length;i++) {
            re.push(i)
        }

        return re
    }

    public static eq<T> (ar1: Array<T>, ar2: Array<T>, compareFn: (a: T, b: T) => boolean): boolean {
        if (ar1.length != ar2.length) return false

        for (let i=0;i<ar1.length;i++) {
            if (!compareFn(ar1[i], ar2[i])) {
                return false
            }
        }

        return true
    }

    public static intersection<T>(ar1: Array<T>, ar2: Array<T>, compareFn: (a: T, b: T) => boolean) : Array<T> {
        return ar1.filter(function (val) { return ArrayTool.indexOf(ar2, val, compareFn) > -1 })
    }
    
    public static shuffle<T>(array: Array<T>) {
        array.sort(() => Math.random() - 0.5);
    }
}

/**
 * public void NOSomethingA()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingB()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingC()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingD()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingE()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingF()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingG()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingH()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingI()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingJ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingK()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingL()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingM()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingN()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingO()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingP()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingQ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingR()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingS()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingT()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingU()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingV()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
 */